package com.chaofan.controller;

import com.chaofan.entity.Books;
import com.chaofan.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @RequestMapping("/allbook")
    public String list(Model model) {
        List<Books> books = bookService.queryAllBook();
        model.addAttribute("list", books);
        return "allbook";
    }

    @GetMapping("/addbook")
    public String toaddBook() {
        return "addbook";
    }

    @PostMapping("/addbook")
    public String addBook(Books books) {
        bookService.addBook(books);
        return "redirect:/book/allbook";
    }

    @GetMapping("/update")
    public String toUpdate(int id, Model model) {
        Books books = bookService.queryBookById(id);
        model.addAttribute("book", books);
        return "update";
    }

    @PostMapping("/update")
    public String update(Books books) {
        bookService.updateBook(books);
        return "redirect:/book/allbook";
    }

    @GetMapping("/delete")
    public String delete(int id) {
        bookService.deleteBook(id);
        return "redirect:/book/allbook";
    }

    @PostMapping("/query")
    public String queryBook(String bookName, Model model) {
        return "";
    }
}
